package helper

import (
	"github.com/gin-gonic/gin"
	"io"
	"net/http"
	"os"
)

// 下载文件
func DownloadFile(remoteUrl string, localPath string) error {
	resp, err := http.Get(remoteUrl)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	// 创建一个文件用于保存
	out, err := os.Create(localPath)
	if err != nil {
		return err
	}
	defer out.Close()

	_, err = io.Copy(out, resp.Body)
	if err != nil {
		return err
	}
	return nil
}

// GetPageIndex 获取分页的页索引
func GetPageIndex(c *gin.Context) int {
	if v := c.Query("pageIndex"); v != "" {
		if iv := ToInt(v); iv > 0 {
			return iv
		}
	}
	return 1
}

// GetPageSize 获取分页的页大小(最大50)
func GetPageSize(c *gin.Context) int {
	if v := c.Query("pageSize"); v != "" {
		if iv := ToInt(v); iv > 0 {
			if iv > 50 {
				iv = 50
			}
			return iv
		}
	}
	return 10
}
